﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Smart.ExerciseCalculationServiceReference;

namespace Smart.Pages
{
    public partial class ExerciseCalc : Smart.UI.BaseClasses.AuthenticatedPage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindgvExercise();
                gvExercises.DataBind();           
            }
        }
        protected void BindgvExercise()
        {
            List<Exercise> list = Global.ExerciseCalculationServiceReference.GetAllExercises().ToList();
            gvExercises.DataSource = list;            
        }

        protected void gvExercises_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            BindgvExercise();
            gvExercises.PageIndex = e.NewPageIndex;
            gvExercises.DataBind();
        }

        protected void Button3_Click(object sender, EventArgs e)
        {
            int ExId;
            int duration;

            try
            {
                ExId = Int32.Parse(tbExerciseId.Text);
                duration = Int32.Parse(tbDuration.Text);

                if (ExId > 0 && duration >= 0)
                {
                    Global.ExerciseCalculationServiceReference.RecordExercise(CurrentUserId, ExId, duration);
                    lblResult.Text = "Успешно въведен запис!";
                }
                else
                    throw new Exception();
            }
            catch (Exception)
            {
                lblResult.Text = "Невалидни стойности за упражнение или времетраене!";
            }
        }

        protected void Button2_Click(object sender, EventArgs e)
        {
            int ExId;
            int duration;

            try
            {
                ExId = Int32.Parse(tbExerciseId.Text);
                duration = Int32.Parse(tbDuration.Text);

                if (ExId > 0 && duration >= 0)
                {
                    double res = Global.ExerciseCalculationServiceReference.CalculateBurnedCalories(ExId, duration);
                    lblResult.Text = "Изгорените калории са: " + res + " Калории.";
                }
                else
                    throw new Exception();
            }
            catch (Exception)
            {
                lblResult.Text = "Невалидни стойности за упражнение или времетраене!";
            }
        }




    }
}